import Taro from '@tarojs/taro'

const TOKEN = ''

const self = {
    _getToken() {
        return TOKEN ? TOKEN : Taro.getStorageSync('token') || ''
    },
    _apiConfig() {
        return process.env.NODE_ENV === 'production'
            ? {
                  API_BASE_URL: 'https://api.krisli.cn' // 生产环境的请求域名
              }
            : {
                  API_BASE_URL: 'http://localhost:3001' // 本地环境的请求域名
              }
    }
}

export const get = async (url, data) => {
    const res = await Taro.request({
        method: 'GET',
        url: self._apiConfig().API_BASE_URL + url,
        data,
        header: {
            Authorization: 'Bearer ' + self._getToken()
        }
    })
    return res.data
}

export const post = async (url, data) => {
    const res = await Taro.request({
        method: 'POST',
        url: self._apiConfig().API_BASE_URL + url,
        data,
        header: {
            Authorization: 'Bearer ' + self._getToken()
        }
    })
    return res.data
}

export const apiConfig = self._apiConfig()
